package br.com.ebc.icontatos.boundary;

import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;

public abstract class AbstractFormManagedbean {

	private boolean messages;
	
	public void showMessage(String message) {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, message, null);
        context.addMessage(null, msg);
        messages = context.getMessages().hasNext();
    }

	public void validationMessage(HtmlInputText text, String message){
		FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null);
        context.addMessage(text.getClientId(context), msg);
	}
	
	public void validationMessage(HtmlInputSecret secret, String message){
		FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null);
        context.addMessage(secret.getClientId(context), msg);
	}
	
	protected Object getBean(String nome){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, nome);
    }
	
	public boolean isMessages() {
		return messages;
	}

	public void setMessages(boolean messages) {
		this.messages = messages;
	}
    
}
